import { Component } from '@angular/core';
import { Input,Output,EventEmitter } from '@angular/core';
import { OnInit,OnDestroy } from '@angular/core';

@Component({
  selector: 'nova-comp-star-rating',
  templateUrl: './comp-star-rating.component.html',
  styleUrls: ['./comp-star-rating.component.scss']
})
export class CompStarRatingComponent implements OnInit,OnDestroy {

  /**
   * 重要数据：当前分值
   */
  @Input()
  score:number = 0 

  @Output()
  scoreChange = new EventEmitter<number>()

  ngOnInit(): void {
    this.startTime = new Date()
    this.makeStarList(this.score)
  }
  startTime:Date = new Date()
  ngOnDestroy():void{
    let endTime = new Date()
    let duration = endTime.getTime() - this.startTime.getTime()
    duration = duration/1000
    console.log("打分已经结束，组件被销毁时候，打印了这段话")
    console.log("打分耗时：",duration)
  }
  starList = [false,false,false,false,false]
  totalScore:number = 5 // 默认的总分

  clickStar(index:number){
    let score = index+1

    let starStatus = this.starList[index]
    if(!starStatus){
      this.score = score
    }
    if(starStatus){
      this.score = 0
    }

    console.log(this.score)
    this.scoreChange.emit(this.score)
    this.makeStarList(this.score)
  }

  makeStarList(score:number){
    let list = []
    for (let index = 0; index < this.totalScore; index++) {
      if(index<score){
        list.push(true)
      }else{
        list.push(false)
      }
    }
    this.starList = list
  }
}
